package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Xieyilist(ctx *gin.Context) {

	var users []models.Xieyi
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type XieyiinfoBody struct {
	ID      int64  `json:"id" gorm:"id"`
	Name    string `json:"name" gorm:"name"`
	Content string `json:"content" gorm:"content"`
}

func Xieyiupdate(ctx *gin.Context) {
	request := XieyiinfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	content := request.Content
	xy := models.Xieyi{
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&xy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Xieyiinfo(ctx *gin.Context) {
	request := XieyiinfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var users models.Xieyi
	data_source.Db.Where("id = ?", id).First(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

func Jieshaolist(ctx *gin.Context) {

	var users []models.Jieshao
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

func Jieshaoupdate(ctx *gin.Context) {
	request := XieyiinfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	content := request.Content
	xy := models.Jieshao{
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&xy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Jieshaoinfo(ctx *gin.Context) {
	request := XieyiinfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var users models.Jieshao
	data_source.Db.Where("id = ?", id).First(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

func Paiminglist(ctx *gin.Context) {

	var users []models.Paiming
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type PaimingBody struct {
	ID      int64   `json:"id" gorm:"id"`
	Paiming int64   `json:"paiming" gorm:"paiming"`
	Name    string  `json:"name" gorm:"name"`
	Daibi   float64 `json:"daibi" gorm:"daibi"`
}

func Paimingadd(ctx *gin.Context) {
	request := PaimingBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	paiming := request.Paiming
	daibi := request.Daibi
	xy := models.Paiming{
		Name:    name,
		Paiming: paiming,
		Daibi:   daibi,
	}
	data_source.Db.Create(&xy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Paimingupdate(ctx *gin.Context) {
	request := PaimingBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	paiming := request.Paiming
	daibi := request.Daibi
	xy := models.Paiming{
		Name:    name,
		Paiming: paiming,
		Daibi:   daibi,
	}
	//data_source.Db.Create(&xy)
	data_source.Db.Where("id = ?", id).Updates(&xy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Paimingdel(ctx *gin.Context) {
	request := XieyiinfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Paiming{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
